/*********************************************************************
*                                                                    *
*                SEGGER Microcontroller GmbH                         *
*        Solutions for real time microcontroller applications        *
*                                                                    *
**********************************************************************
*                                                                    *
* C-file generated by:                                               *
*                                                                    *
*        GUI_Builder for emWin version 6.10                          *
*        Compiled Dec 19 2019, 16:36:21                              *
*        (c) 2019 Segger Microcontroller GmbH                        *
*                                                                    *
**********************************************************************
*                                                                    *
*        Internet: www.segger.com  Support: support@segger.com       *
*                                                                    *
**********************************************************************
*/

// USER START (Optionally insert additional includes)
#include "widget_set.h"
#include "ui_touch.h"
#include "key_board.h"
// USER END

#include "DIALOG.h"

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
#define ID_WINDOW_0 (GUI_ID_USER + 0x00)
#define ID_EDIT_0 (GUI_ID_USER + 0x01)
#define ID_EDIT_1 (GUI_ID_USER + 0x02)

#define ID_BUTTON_7 (GUI_ID_USER + 0x03)
#define ID_BUTTON_8 (GUI_ID_USER + 0x04)
#define ID_BUTTON_9 (GUI_ID_USER + 0x05)
#define ID_BUTTON_ENT (GUI_ID_USER + 0x06)
#define ID_BUTTON_4 (GUI_ID_USER + 0x07)
#define ID_BUTTON_5 (GUI_ID_USER + 0x08)
#define ID_BUTTON_6 (GUI_ID_USER + 0x09)
#define ID_BUTTON_1 (GUI_ID_USER + 0x0A)
#define ID_BUTTON_2 (GUI_ID_USER + 0x0B)
#define ID_BUTTON_3 (GUI_ID_USER + 0x0C)
#define ID_BUTTON_DEL (GUI_ID_USER + 0x0D)
#define ID_BUTTON_0 (GUI_ID_USER + 0x0E)
#define ID_BUTTON_DOT (GUI_ID_USER + 0x0F)
#define ID_BUTTON_NEV (GUI_ID_USER + 0x10)
#define ID_BUTTON_X (GUI_ID_USER + 0x11)



// USER START (Optionally insert additional defines)
key_board_ctr_type keyboard_ctr;
extern GUI_CONST_STORAGE GUI_FONT GUI_FontSongTi48_ASCII;
extern GUI_CONST_STORAGE GUI_FONT GUI_FontConsolas30;
// USER END

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

// USER START (Optionally insert additional static data)
extern GUI_CONST_STORAGE GUI_BITMAP bmKEY_480272;

#define KEY_X0 35
#define KEY_Y0 81
#define KEY_W  98
#define KEY_H  40
#define KEY_X_SPC 105
#define KEY_Y_SPC 45

// USER END

/*********************************************************************
*
*       _aDialogCreate
*/
static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
	{ WINDOW_CreateIndirect, "Window", ID_WINDOW_0, (800 - 480) / 2, (480 - 272), 480, 272, WM_CF_MEMDEV_ON_REDRAW, 0x0, 0 },
	{ TEXT_CreateIndirect, "Edit", ID_EDIT_0, 200, 45, 220, 26, 0, 0x64, 0 },
	{ TEXT_CreateIndirect, "RANG", ID_EDIT_1, 50, 8, 370, 26, 0, 0x64, 0 },

	// USER START (Optionally insert additional widgets)
	{ BUTTON_CreateIndirect, "7", ID_BUTTON_7, KEY_X0 + 0 * KEY_X_SPC, KEY_Y0 + 0 * KEY_Y_SPC, KEY_W, KEY_H, 0, 0x0, 0 },
	{ BUTTON_CreateIndirect, "8", ID_BUTTON_8, KEY_X0 + 1 * KEY_X_SPC, KEY_Y0 + 0 * KEY_Y_SPC, KEY_W, KEY_H, 0, 0x0, 0 },
	{ BUTTON_CreateIndirect, "0", ID_BUTTON_9, KEY_X0 + 2 * KEY_X_SPC, KEY_Y0 + 0 * KEY_Y_SPC, KEY_W, KEY_H, 0, 0x0, 0 },
	{ BUTTON_CreateIndirect, "ENT", ID_BUTTON_ENT, KEY_X0 + 3 * KEY_X_SPC, KEY_Y0 + 0 * KEY_Y_SPC, KEY_W, KEY_H * 2 + (KEY_Y_SPC - KEY_H), 0, 0x0, 0 },

	{ BUTTON_CreateIndirect, "4", ID_BUTTON_4, KEY_X0 + 0 * KEY_X_SPC, KEY_Y0 + 1 * KEY_Y_SPC, KEY_W, KEY_H, 0, 0x0, 0 },
	{ BUTTON_CreateIndirect, "5", ID_BUTTON_5, KEY_X0 + 1 * KEY_X_SPC, KEY_Y0 + 1 * KEY_Y_SPC, KEY_W, KEY_H, 0, 0x0, 0 },
	{ BUTTON_CreateIndirect, "6", ID_BUTTON_6, KEY_X0 + 2 * KEY_X_SPC, KEY_Y0 + 1 * KEY_Y_SPC, KEY_W, KEY_H, 0, 0x0, 0 },
	//{ BUTTON_CreateIndirect, "Button", ID_BUTTON_1, KEY_X0 + 3 * KEY_X_SPC, KEY_Y0 + 1 * KEY_Y_SPC, KEY_W, KEY_H, 0, 0x0, 0 },

	{ BUTTON_CreateIndirect, "1", ID_BUTTON_1, KEY_X0 + 0 * KEY_X_SPC, KEY_Y0 + 2 * KEY_Y_SPC, KEY_W, KEY_H, 0, 0x0, 0 },
	{ BUTTON_CreateIndirect, "2", ID_BUTTON_2, KEY_X0 + 1 * KEY_X_SPC, KEY_Y0 + 2 * KEY_Y_SPC, KEY_W, KEY_H, 0, 0x0, 0 },
	{ BUTTON_CreateIndirect, "3", ID_BUTTON_3, KEY_X0 + 2 * KEY_X_SPC, KEY_Y0 + 2 * KEY_Y_SPC, KEY_W, KEY_H, 0, 0x0, 0 },
	{ BUTTON_CreateIndirect, "DEL", ID_BUTTON_DEL, KEY_X0 + 3 * KEY_X_SPC, KEY_Y0 + 2 * KEY_Y_SPC, KEY_W, KEY_H, 0, 0x0, 0 },

	{ BUTTON_CreateIndirect, "0", ID_BUTTON_0, KEY_X0 + 0 * KEY_X_SPC, KEY_Y0 + 3 * KEY_Y_SPC, KEY_W, KEY_H, 0, 0x0, 0 },
	{ BUTTON_CreateIndirect, ".", ID_BUTTON_DOT, KEY_X0 + 1 * KEY_X_SPC, KEY_Y0 + 3 * KEY_Y_SPC, KEY_W, KEY_H, 0, 0x0, 0 },
	{ BUTTON_CreateIndirect, "-", ID_BUTTON_NEV, KEY_X0 + 2 * KEY_X_SPC, KEY_Y0 + 3 * KEY_Y_SPC, KEY_W, KEY_H, 0, 0x0, 0 },
	{ BUTTON_CreateIndirect, "X", ID_BUTTON_X, KEY_X0 + 3 * KEY_X_SPC, KEY_Y0 + 3 * KEY_Y_SPC, KEY_W, KEY_H, 0, 0x0, 0 },
	// USER END
};

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/

// USER START (Optionally insert additional static code)
void button_skin_at_keyboard(WM_HMEM hItem, short x, short y);
// USER END

static void key_handle(WM_MESSAGE * pMsg, char key_val)
{
	WM_HWIN hItem;
	char condition = 0;

	condition = (key_val >= '0') && (key_val <= '9');
	condition |= key_val == '-';
	condition |= key_val == '.';

	if (condition)
	{
		if (keyboard_ctr.hItemType == KB_VALUE_MODE_NUM)
			ui_num_text_input_add(&keyboard_ctr.ui, key_val);
		else
			ui_text_input_add(&keyboard_ctr.ui, key_val);
	}

	switch (key_val)
	{
		case GUI_KEY_DELETE:
			if (keyboard_ctr.hItemType == KB_VALUE_MODE_NUM)
				ui_num_text_input_del(&keyboard_ctr.ui);
			else
				ui_text_input_del(&keyboard_ctr.ui);
			break;
		case GUI_KEY_ENTER:
			keyboard_ctr.b_in_end = 1;
			if (keyboard_ctr.hItemType == KB_VALUE_MODE_NUM)
			{
				if (keyboard_ctr.ui.len)
				{
					if (keyboard_ctr.ui.buff[keyboard_ctr.ui.index_next - 1] == '.')
					{
						ui_num_text_input_del(&keyboard_ctr.ui);
					}
				}
			}
			if (keyboard_ctr.key_board_call_back)
			{
				keyboard_ctr.key_board_call_back();
			}
			break;
		case GUI_KEY_END:
			keyboard_ctr.b_in_end = 1;
			break;
	}

	hItem = WM_GetDialogItem(pMsg->hWin, ID_EDIT_0);
	//
	if (!keyboard_ctr.b_password_disp)
	{
		TEXT_SetText(hItem, keyboard_ctr.buff);
	}
	else
	{
		char pwd[64], len;
		len = strlen(keyboard_ctr.buff);
		memset(pwd, '*', len);
		pwd[len] = 0;
		TEXT_SetText(hItem, pwd);
	}
}

/*********************************************************************
*
*       _cbDialog
*/
static void _cbDialog(WM_MESSAGE * pMsg)
{
	WM_HWIN hItem;
	int     NCode;
	int     Id;
	char rang_str[64];

	// USER START (Optionally insert additional variables)
	int index;
	// USER END

	switch (pMsg->MsgId)
	{
		case WM_INIT_DIALOG:
			//
			// Initialization of 'Edit'
			//
			hItem = WM_GetDialogItem(pMsg->hWin, ID_EDIT_0);
			//EDIT_SetFont(hItem, GUI_FONT_20_ASCII);
			//EDIT_SetBkColor(hItem, EDIT_CI_CURSOR, 0xFF << 24);
			//EDIT_SetBkColor(hItem, EDIT_CI_ENABLED, 0xFF << 24);
			//EDIT_SetTextColor(hItem, EDIT_CI_CURSOR, GUI_LIGHTGREEN);
			//EDIT_SetTextColor(hItem, EDIT_CI_ENABLED, GUI_LIGHTGREEN);
			//EDIT_SetTextAlign(hItem, GUI_TA_VCENTER | GUI_TA_RIGHT);
			TEXT_SetFont(hItem, &GUI_FontConsolas30);
			TEXT_SetTextColor(hItem, GUI_WHITE);
			TEXT_SetTextAlign(hItem, GUI_TA_VCENTER | GUI_TA_RIGHT);
			TEXT_SetText(hItem, rang_str);
			TEXT_SetText(hItem, "");

			// USER START (Optionally insert additional code for further widget initialization)

			for (index = ID_BUTTON_7; index <= ID_BUTTON_X; index++)
			{
				hItem = WM_GetDialogItem(pMsg->hWin, index);
				button_skin_at_keyboard(hItem, dialog_size_get(index));
			}

			hItem = WM_GetDialogItem(pMsg->hWin, ID_EDIT_1);
			if (keyboard_ctr.ui.b_rang_en)
			{
				int index;
				if (keyboard_ctr.name)
				{
					index = strlen(keyboard_ctr.name);
					memcpy(rang_str, keyboard_ctr.name, index);
					rang_str[index++] = ':';
					rang_str[index++] = ' ';
				}
				else
				{
					index = 0;
				}
				i32_to_string(&rang_str[index], keyboard_ctr.ui.min, keyboard_ctr.ui.in_pn, 0x01);
				index = strlen(rang_str);
				rang_str[index++] = ' ';
				rang_str[index++] = '~';
				rang_str[index++] = ' ';
				i32_to_string(&rang_str[index], keyboard_ctr.ui.max, keyboard_ctr.ui.in_pn, 0x01);
				TEXT_SetFont(hItem, GUI_FONT_20_ASCII);
				TEXT_SetTextColor(hItem, GUI_ORANGE);
				TEXT_SetTextAlign(hItem, GUI_TA_VCENTER | GUI_TA_RIGHT);
				TEXT_SetText(hItem, rang_str);

			}
			else
			{
				TEXT_SetText(hItem, "  ");
			}


			// USER END
			break;
		case WM_PAINT:
			GUI_Clear();
			GUI_DrawBitmap(&bmKEY_480272, 0, 0);
			break;
		case WM_NOTIFY_PARENT:
			Id = WM_GetId(pMsg->hWinSrc);
			NCode = pMsg->Data.v;
			switch (Id)
			{
				case ID_EDIT_0: // Notifications sent by 'Edit'
					switch (NCode)
					{
						case WM_NOTIFICATION_CLICKED:
							// USER START (Optionally insert code for reacting on notification message)
							// USER END
							break;
						case WM_NOTIFICATION_RELEASED:
							// USER START (Optionally insert code for reacting on notification message)
							// USER END
							break;
						case WM_NOTIFICATION_VALUE_CHANGED:
							// USER START (Optionally insert code for reacting on notification message)
							// USER END
							break;
							// USER START (Optionally insert additional code for further notification handling)
							// USER END
					}
					break;
				case ID_BUTTON_0: // Notifications sent by 'Button'

					switch (NCode)
					{
						case WM_NOTIFICATION_CLICKED:
							// USER START (Optionally insert code for reacting on notification message)
							// USER END
							break;
						case WM_NOTIFICATION_RELEASED:
							// USER START (Optionally insert code for reacting on notification message)
							key_handle(pMsg, '0');
							// USER END
							break;
							// USER START (Optionally insert additional code for further notification handling)
							// USER END
					}
					break;
					// USER START (Optionally insert additional code for further Ids)
				case ID_BUTTON_1: // Notifications sent by 'Button'
					if (NCode == WM_NOTIFICATION_CLICKED)
					{
						key_handle(pMsg, '1');
					}
					break;
				case ID_BUTTON_2: // Notifications sent by 'Button'
					if (NCode == WM_NOTIFICATION_CLICKED)
					{
						key_handle(pMsg, '2');
					}
					break;
				case ID_BUTTON_3: // Notifications sent by 'Button'
					if (NCode == WM_NOTIFICATION_CLICKED)
					{
						key_handle(pMsg, '3');
					}
					break;
				case ID_BUTTON_4: // Notifications sent by 'Button'
					if (NCode == WM_NOTIFICATION_CLICKED)
					{
						key_handle(pMsg, '4');
					}
					break;
				case ID_BUTTON_5: // Notifications sent by 'Button'
					if (NCode == WM_NOTIFICATION_CLICKED)
					{
						key_handle(pMsg, '5');
					}
					break;
				case ID_BUTTON_6: // Notifications sent by 'Button'
					if (NCode == WM_NOTIFICATION_CLICKED)
					{
						key_handle(pMsg, '6');
					}
					break;
				case ID_BUTTON_7: // Notifications sent by 'Button'
					if (NCode == WM_NOTIFICATION_CLICKED)
					{
						key_handle(pMsg, '7');
					}
					break;
				case ID_BUTTON_8: // Notifications sent by 'Button'
					if (NCode == WM_NOTIFICATION_CLICKED)
					{
						key_handle(pMsg, '8');
					}
					break;
				case ID_BUTTON_9: // Notifications sent by 'Button'
					if (NCode == WM_NOTIFICATION_CLICKED)
					{
						key_handle(pMsg, '9');
					}
					break;
				case ID_BUTTON_DOT: // Notifications sent by 'Button'
					if (NCode == WM_NOTIFICATION_CLICKED)
					{
						key_handle(pMsg, '.');
					}
					break;
				case ID_BUTTON_NEV: // Notifications sent by 'Button'
					if (NCode == WM_NOTIFICATION_CLICKED)
					{
						key_handle(pMsg, '-');
					}
					break;
				case ID_BUTTON_ENT: // Notifications sent by 'Button'
					if (NCode == WM_NOTIFICATION_RELEASED)
					{
						key_handle(pMsg, GUI_KEY_ENTER);
					}
					break;
				case ID_BUTTON_X: // Notifications sent by 'Button'
					if (NCode == WM_NOTIFICATION_RELEASED)
					{
						key_handle(pMsg, GUI_KEY_END);
					}
					break;
				case ID_BUTTON_DEL: // Notifications sent by 'Button'
					if (NCode == WM_NOTIFICATION_CLICKED)
					{
						key_handle(pMsg, GUI_KEY_DELETE);
					}
					break;
					// USER END
			}
			break;
			// USER START (Optionally insert additional message handling)
			// USER END
		default:
			WM_DefaultProc(pMsg);
			break;
	}
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/

/*******************************************************************************
* @brief  key board poll
* \param[in] none
* \retval: none
*******************************************************************************/
void key_board_manager(void)
{
	if (keyboard_ctr.b_in_end)
	{
		keyboard_ctr.b_busy = 0;
		keyboard_ctr.b_in_end = 0;
		GUI_EndDialog(keyboard_ctr.h_win_keyboard, 0);
	}

	if (keyboard_ctr.b_num_keyboard_req)
	{
		ui_text_input_reset(&keyboard_ctr.ui, (void *)keyboard_ctr.buff, 60);
		keyboard_ctr.b_num_keyboard_req = 0;
		keyboard_ctr.b_ascii_keyboard_req = 0;
		keyboard_ctr.b_busy = 1;
		keyboard_ctr.h_win_keyboard = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbDialog, keyboard_ctr.hItemWindow, 0, 0);
	}
}

/*******************************************************************************
* @brief  key board req
* \param[in] none
* \retval: ok 1 or fail 0
*******************************************************************************/
char num_key_board_open_req(void)
{
	if (!keyboard_ctr.b_busy)
	{
		keyboard_ctr.b_num_keyboard_req = 1;
		return 1;
	}

	return 0;
}

// USER END

/*************************** End of file ****************************/
